package com.tkx.dxb.service.article.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkx.dxb.common.base.result.R;
import com.tkx.dxb.service.article.entity.Comment;
import com.tkx.dxb.service.article.entity.vo.CommentQueryVo;
import com.tkx.dxb.service.article.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author victor
 * @since 2021-04-07
 */
@Api(tags = "评论")
@RestController
@RequestMapping("/admin/article/comment")
public class CommentController {

    private CommentService commentService;

    @Autowired
    public void setCommentService(CommentService commentService){
        this.commentService = commentService;
    }

    @ApiOperation("所有评论列表")
    @GetMapping("list")
    public R listAll(){
        List<Comment> list = commentService.list();
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "根据ID删除评论",notes = "根据ID删除评论")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "文章ID",required = true) @PathVariable String id) {
        boolean result = commentService.removeById(id);
        if (result) {
            return R.ok().message("删除成功！");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "根据ID列表删除评论")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "评论ID列表",required = true)
            @RequestBody List<String> idList) {
        boolean result = commentService.removeByIds(idList);
        if (result) {
            return R.ok().message("删除成功！");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("评论分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam("评论列表查询对象") CommentQueryVo commentQueryVo) {
        Page<Comment> pageParam = new Page<>(page, limit);
        /**
         * queryWrapper 查询条件，无查询对象省略
         */
        IPage<Comment> pageModel = commentService.selectPage(pageParam, commentQueryVo);
        /**
         * getRecords 获取数据
         * getTotal   获取总页数
         */
        List<Comment> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
}

